﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Collections.Pooled;
using FieldTools.Util;

namespace FieldTools.Client.Model.Window
{
    /// <summary>
    /// 图片浏览器实体
    /// </summary>
    public class PicShowModel : IDisposable
    {
        private bool disposedValue;

        /// <summary>
        /// 图片数据集合
        /// </summary>
        public PooledList<Pic>? Pics { get; set; }
        /// <summary>
        /// 预加载
        /// </summary>
        public bool PreLoadStatus { get; set; } = false;
        /// <summary>
        /// 原图状态
        /// </summary>
        public bool OriginalStatus { get; set; } = false;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    Pics?.Dispose();
                }

                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~PicShowModel()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }
        /// <summary>
        /// 资源释放
        /// </summary>
        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }
    /// <summary>
    /// 自定义图片数据
    /// </summary>
    public class Pic : IDisposable
    {
        private bool disposedValue;

        /// <summary>
        /// 标题
        /// </summary>
        public string? Title { get; set; }
        /// <summary>
        /// 来自
        /// </summary>
        public string? From { get; set; }
        /// <summary>
        /// 主题
        /// </summary>
        public string? Subject { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public string? CreatedAt { get; set; }
        /// <summary>
        /// 创建人，网名
        /// </summary>
        public string? CreatedBy { get; set; }
        /// <summary>
        /// 高度
        /// </summary>
        public int? Height { get; set; }
        /// <summary>
        /// 宽度
        /// </summary>
        public int? Width { get; set; }
        /// <summary>
        /// 字节大小
        /// </summary>
        public long? Size { get; set; }
        /// <summary>
        /// 描述内容
        /// </summary>
        public string? Content { get; set; }
        /// <summary>
        /// 图片索引
        /// </summary>
        public int Index { get; set; } = 0;
        /// <summary>
        /// 图片数据
        /// </summary>
        public BitmapFrameEX? BitmapFrameE { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    BitmapFrameE?.Dispose();
                }
                Title = null;
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~Pic()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }
    }

    /// <summary>
    /// 重写BitmapFrame
    /// </summary>
    public class BitmapFrameEX : BitmapFrame ,IDisposable
    {
        /// <summary>
        /// 网址构建
        /// </summary>
        /// <param name="url"></param>
        public BitmapFrameEX(string url)
        {
            BaseUri = new Uri(url);
        }
        /// <summary>
        /// Uri构建
        /// </summary>
        /// <param name="url"></param>
        public BitmapFrameEX(Uri url)
        {
            BaseUri = url;
        }
        /// <inheritdoc/>
        public override Uri? BaseUri { get; set; }

        /// <inheritdoc/>
        public override ReadOnlyCollection<ColorContext> ColorContexts => throw new NotImplementedException();
        /// <inheritdoc/>
        public override BitmapDecoder? Decoder { get; }
        private Image? img;
        /// <summary>
        /// 图片
        /// </summary>
        public Image? Img
        {
            get
            {
                if(BaseUri == null)
                {
                    img = null;
                    return null;
                }
                if (img != null)
                {
                    return img;
                }
                try
                {
                    var ste = WebRequest.Create(BaseUri).GetResponse().GetResponseStream();
                    img = Bitmap.FromStream(ste);
                    return img;
                }
                catch (Exception)
                {
                    return null;
                }
            }
            set
            {
                img = value;
            }
        }
        private BitmapSource? bitmapImageSource;
        /// <summary>
        /// 原图
        /// </summary>
        public BitmapSource? BitmapImageSource
        {
            get
            {
                if (BaseUri == null)
                {
                    img = null;
                    return null;
                }
                if (Img == null) return null;
                if(bitmapImageSource == null)
                {
                    Image image = Img;
                    Bitmap bmp = new(Img);
                    bitmapImageSource = GetBitMapSourceFromBitmap(bmp);
                }
                return bitmapImageSource;
            }
        }
        private BitmapSource? thumbnail;
        private bool disposedValue;

        /// <summary>
        /// 缩略图
        /// </summary>
        public override BitmapSource? Thumbnail
        {
            get
            {
                if (BaseUri == null)
                {
                    img = null;
                    return null;
                }
                if (Img == null) return null;
                if(thumbnail == null)
                {
                    Image image = Img.Compress(300);
                    Bitmap bmp = new(image);
                    thumbnail = GetBitMapSourceFromBitmap(bmp);
                }
                return thumbnail;
            }
        }

        /// <inheritdoc/>
        public override InPlaceBitmapMetadataWriter CreateInPlaceBitmapMetadataWriter()
        {
            throw new NotImplementedException();
        }

        /// <inheritdoc/>
        protected override Freezable CreateInstanceCore()
        {
            throw new NotImplementedException();
        }

        [DllImport("gdi32")]
        static extern int DeleteObject(IntPtr o);


        /// <summary>
        /// Bitmap->BitmapSource
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static BitmapSource GetBitMapSourceFromBitmap(Bitmap bitmap)
        {
            IntPtr intPtrl = bitmap.GetHbitmap();
            BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPtrl,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
            _ = DeleteObject(intPtrl);
            return bitmapSource;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: 释放托管状态(托管对象)
                    img?.Dispose();
                }
                img = null;
                thumbnail = null;
                bitmapImageSource = null;
                // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                // TODO: 将大型字段设置为 null
                disposedValue = true;
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~BitmapFrameEX()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

       
    }
}
